package com.socialsoftware.ui.Contact;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class SidebarIndexView extends View {
    private String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 
                               "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    private Paint paint = new Paint();
    private int selectedIndex = -1;
    private OnLetterSelectedListener listener;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private TextView indicatorTextView;
    
    public SidebarIndexView(Context context) {
        super(context);
        init();
    }
    
    public SidebarIndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public SidebarIndexView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    private void init() {
        paint.setAntiAlias(true);
        paint.setTextSize(30);
        paint.setTextAlign(Paint.Align.CENTER);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        int height = getHeight();
        int width = getWidth();
        int singleHeight = height / letters.length;
        
        for (int i = 0; i < letters.length; i++) {
            if (i == selectedIndex) {
                paint.setColor(Color.parseColor("#3399ff"));
                paint.setFakeBoldText(true);
            } else {
                paint.setColor(Color.parseColor("#666666"));
                paint.setFakeBoldText(false);
            }
            
            float xPos = width / 2;
            float yPos = singleHeight * i + singleHeight / 2 + paint.getTextSize() / 2;
            canvas.drawText(letters[i], xPos, yPos, paint);
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float y = event.getY();
                int index = (int) (y / getHeight() * letters.length);
                
                if (index >= 0 && index < letters.length) {
                    if (index != selectedIndex) {
                        selectedIndex = index;
                        if (listener != null) {
                            listener.onLetterSelected(letters[index]);
                        }
                        
                        if (indicatorTextView != null) {
                            indicatorTextView.setVisibility(View.VISIBLE);
                            indicatorTextView.setText(letters[index]);
                        }
                        
                        // Scroll to position if recyclerView is set
                        if (recyclerView != null && layoutManager != null) {
                            int position = getPositionForLetter(letters[index]);
                            if (position != -1) {
                                layoutManager.scrollToPositionWithOffset(position, 0);
                            }
                        }
                    }
                    invalidate();
                }
                return true;
            case MotionEvent.ACTION_UP:
                selectedIndex = -1;
                invalidate();
                if (indicatorTextView != null) {
                    indicatorTextView.setVisibility(View.GONE);
                }
                return true;
        }
        return super.onTouchEvent(event);
    }
    
    private int getPositionForLetter(String letter) {
        // This should be implemented by the adapter
        if (listener != null) {
            return listener.getPositionForLetter(letter);
        }
        return -1;
    }
    
    public void setOnLetterSelectedListener(OnLetterSelectedListener listener) {
        this.listener = listener;
    }
    
    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        this.layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    }
    
    public void setIndicatorTextView(TextView textView) {
        this.indicatorTextView = textView;
    }
    
    public interface OnLetterSelectedListener {
        void onLetterSelected(String letter);
        int getPositionForLetter(String letter);
    }
} 